﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.Windows.Forms;
using System.Xml; 

namespace Quanlynhasach.hethongluutru
{
    class database
    {
        protected static string _connectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=QLNS.mdb";
        static OleDbConnection connection;
        public static void OpenConnection()
        {
            try
            {
                connection = new OleDbConnection(_connectionString);
                connection.Open();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Không thể kết nối được với Server\n"
+ ex.Message, "Lỗi chương trình");
                //Application.Exit(); 
            }

        }
        public static void CloseConnection()
        {
            if (connection != null)
            {
                connection.Close();
            }
        }
        public static DataTable ExcuQuery(string sql)
        {


            OpenConnection();
            DataTable dt = new DataTable();
            OleDbCommand command = connection.CreateCommand();
            command.CommandText = sql;
            OleDbDataAdapter adapter = new OleDbDataAdapter();
            adapter.SelectCommand = command;
            adapter.Fill(dt);
            CloseConnection();
            return dt;
            
        }
        public static void ExcuNonQuery(string sql)
        {
            OpenConnection();
            OleDbCommand command = connection.CreateCommand();
            command.CommandText = sql;
            command.ExecuteNonQuery();
            CloseConnection();
        }
        //Lay 1 gia tri du lieu ra 
        public static object ExecuteScalar(string sql)
        {
            OpenConnection();
            OleDbCommand command = connection.CreateCommand();

            object o = command.ExecuteScalar();
            CloseConnection();
            return o;
        }

       
    }
}
